$(function () {
  loadGridTable();
});

function loadGridTable() {
  $("#item_list_table").jqGrid({
    url: "items.action?approveStatus="+$("#approve_status").val(),
    datatype: "json",
    height: 520,
    width: 510,
    colNames: ["编号", "图片", "名称", "价格", "pic_url", "frank_title"],
    colModel: [{
      name: "iid",
      index: "iid",
      hidden: true
    },
    {
      name: "pic_url",
      index: "pic_url",
      formatter:formatItemPic,
      width: "60",
      align:"center",
      sortable: false
    },
    {
      name: "title",
      index: "title",
      formatter:formateadorLink,
      width: "350"
    },
    {
      name: "price",
      index: "price",
      sorttype: "int",
      width: "50"
    },
    {
      name: "pic_url",
      index: "pic_url",
      hidden: true
    },
    {
      name: "frank_title",
      index: "frank_title",
      hidden: true
    }
    ],
    sortname: "delist_time",
    sortorder: "desc",
    viewrecords: true,
    rowNum: 8,
    rowList: [8, 15, 25],
    multiselect: true,
    multiselectWidth: 30,
    loadComplete: function () {
    	setGridTableSelection();
		setImageCellHeight('item_list_table');
    },
    onSelectRow: function (rowid, status) {
      if (rowid === null) {
        return;
      }
      var ret = $("#item_list_table").jqGrid("getRowData", rowid);
      var oldsrc = $("#" + ret.iid + " img").attr("src");
      if (status) {
        if (oldsrc!==undefined && oldsrc!==null) return;
     	
        var title_html = buildSimpleTitle(ret);
        var onClickButtonHtml = "<input type='button' class='btn2 pbtn_60' onmouseout=\"this.style.borderColor='#dcdcdc'\" onmouseover=\"this.style.borderColor='#f76b00'\" value='删除' style=\"border-color: rgb(220, 220, 220);\" onclick=\"delete_item('" + ret.iid + "')\">";
        var result = "<div id='" + ret.iid +  "' class='bePromoted' price='" + ret.price + "' item_title='"+ret.frank_title+"' title='"+ret.frank_title+"'> <img src='" + ret.pic_url + "'/><p>" +onClickButtonHtml+ "</p></div>";
        $("#sortable_item_container").append(result);
      } else {
        $("#" + ret.iid).remove();
      }
    },
    onSelectAll: function (ids, status) {
      var action;
      if (ids === null || ids.length === 0) {
        return;
      }
      if (status) {
        for (var i = 0; i < ids.length; i=i+1) {
          var ret = $("#item_list_table").jqGrid("getRowData", ids[i]);
          var oldsrc = $("#" + ret.iid + " img").attr("src");
          
          if (oldsrc===undefined || oldsrc === null) {
            var title_html = buildSimpleTitle(ret);
            var onClickButtonHtml = "<input type='button' class='btn2 pbtn_60' onmouseout=\"this.style.borderColor='#dcdcdc'\" onmouseover=\"this.style.borderColor='#f76b00'\" value='删除' style=\"border-color: rgb(220, 220, 220);\" onclick=\"delete_item('" + ret.iid + "')\">";
	        var result = "<div id='" + ret.iid +  "' class='bePromoted' price='" + ret.price + "' item_title='"+ret.frank_title+"'> <img src='" + ret.pic_url + "'/><p>" +onClickButtonHtml+ "</p></div>";
	        $("#sortable_item_container").append(result);
          }
          insert_iids(ret.iid);
        }
      } else {
        for (i = 0; i < ids.length; i=i+1) {
          ret = $("#item_list_table").jqGrid("getRowData", ids[i]);
          $("#" + ret.iid).remove();
        }
      }
      //sub_itemchoose_action($("#iids_hidden").val());
    },
    jsonReader: {
      root: "dataset",
      page: "pager.curPageNo",
      total: "pager.totalPages",
      records: "pager.totalCount",
      repeatitems: false
    },
    prmNames: {
      rows: "pager.pageSize",
      page: "pager.curPageNo",
      sort: "pager.orderBy",
      order: "pager.order"
    },
    pager: "#item_list_pager"
  });
}

$(function() {
	$('#item_key').keypress(function(event) {
	    if ((event.keyCode == 13)) {// 在这里接收的是Enter事件,S的ASCII码为13。
	       doSearch();
	    }
  	});
    
  	$('#itm_search_btn').click(function(){
    	doSearch();
  	});
});


$(function () {
	$("#approve_status").change(function () {
    	doSearch();
  	});
  
  	$("#sellerCats").change(function () {
    	doSearch();
  	});
});

function doSearch() {
  var url = "items.action?";
  url += "approveStatus=" + $("#approve_status").val();
  url += "&searchKey="+encodeURI(encodeURI($("#item_key").val()));
  url += "&sellerCids="+$("#sellerCats").val();
  
  $("#item_list_table").jqGrid("setGridParam", {
    url: url,
    page: 1
  }).trigger("reloadGrid");
}
